package com.smartwebee.android.blespp.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;

import com.smartwebee.android.blespp.R;

import java.io.File;
import java.io.IOException;

public class FileUtils {
    public static final String LOG_TAG = "FileUtils";
    public final static String saveDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/hsbluetooth/";
    public final static int STORAGE_CODE = 100;
    // 生成文件
    public static File makeFilePath(String filePath, String fileName) {
        File file = null;
        makeRootDirectory(filePath);
        try {
            file = new File(filePath + fileName);
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }

    // 生成文件夹
    public static void makeRootDirectory(String filePath) {
        File file = null;
        try {
            file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean checkStoragePermissions(Activity activity) {
        int permissionStorage = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        String[] pArray = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (permissionStorage != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity, pArray, STORAGE_CODE);
            return false;
        }
        return true;
    }
}
